package com.ericsson.enap.modules.kpi.thread;

import com.ericsson.common.util.AviatorUtil;
import com.ericsson.common.util.FileUtil;
import com.ericsson.common.util.FormulaConvertImsFive;
import com.ericsson.common.util.SpringUtils;
import com.ericsson.enap.modules.kpi.entity.*;
import com.ericsson.enap.modules.kpi.service.EnipKpiFileProcess;
import com.googlecode.aviator.Expression;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.slf4j.Logger;
import org.springframework.data.mongodb.core.MongoTemplate;

import java.io.File;
import java.util.*;

/**
 * scscfOrigSuccessfulEstablishedInvitePerAccess.{AccessType=startingwith3GPP-NR}+scscfTermSuccessfulEstablishedInvitePerAccess.{AccessType=startingwith3GPP-NR}
 * <p>
 * scscfOrigSuccessfulEstablishedInvitePerAccess__N4__AccessType__N2__startingwith3GPP__N3__NR, scscfTermSuccessfulEstablishedInvitePerAccess__N4__AccessType__N2__startingwith3GPP__N3__NR
 * -------------------------------------------------------------------------------------
 * <p>
 * sum{sbgSipRegStatRejRegCnt}(@AccessNetPcscf)+sum{sbgSipEmerRegStatRejRegCnt}(@AccessNetPcscf)
 * <p>
 * [sbgSipRegStatRejRegCnt__N1__AccessNetPcscf, sbgSipEmerRegStatRejRegCnt__N1__AccessNetPcscf]
 * -------------------------------------------------------------------------------------
 * <p>
 * (scscfOrigSuccessfulEstablishedInvitePerAccess.{access-type=notstartingwith3GPP})
 * <p>
 * [scscfOrigSuccessfulEstablishedInvitePerAccess__N4__access__N3__type__N2__notstartingwith3GPP]
 * -------------------------------------------------------------------------------------
 * <p>
 * scscfOrigAttemptedInvitePerMediaAndAccess.{AccessType=notstartingwith3GPP}.{MediaType=video}
 * <p>
 * [scscfOrigAttemptedInvitePerMediaAndAccess__N4__AccessType__N2__notstartingwith3GPP__N4__MediaType__N2__video]
 * -------------------------------------------------------------------------------------
 * <p>
 * scscfOrigSuccessfulEstablishedInvitePerAccess.{AccessType=startingwith3GPP-NR}
 * <p>
 * [scscfOrigSuccessfulEstablishedInvitePerAccess__N4__AccessType__N2__startingwith3GPP__N3__NR]
 * -------------------------------------------------------------------------------------
 * <p>
 * scscfOrigAttemptedInvitePerMediaAndAccess.{AccessType=startingwith3GPP-E-UTRAN}.{MediaType=video}
 * <p>
 * [scscfOrigAttemptedInvitePerMediaAndAccess__N4__AccessType__N2__startingwith3GPP__N3__E__N3__UTRAN__N4__MediaType__N2__video]
 * -------------------------------------------------------------------------------------
 * <p>
 * scscfOrigAttemptedInvitePerMediaAndAccess.{AccessType=startingwith3GPP-E-UTRAN}.{MediaType=video|MediaType=audio}
 * <p>
 * [scscfOrigAttemptedInvitePerMediaAndAccess__N4__AccessType__N2__startingwith3GPP__N3__E__N3__UTRAN__N4__MediaType__N2__video__N5__MediaType__N2__audio]
 * IMS五分钟KPI数据处理线程
 */
public class ImsFiveKpiDataProcessThread implements Runnable {

    private EnipKpiFileProcess kpiFileProcess;
    private Logger log;
    private MongoTemplate mongoTemplate;
    private List<EnipFormula> formulas;
    private String typeStr;
    private Date date;
    private ImsFiveKpi imsFiveKpi;
    private String imskpiDownloadDir;

    public ImsFiveKpiDataProcessThread(ImsFiveKpi imsFiveKpi, String typeStr, List<EnipFormula> formulas, Date date, Logger log, String imskpiDownloadDir) {
        this.kpiFileProcess = SpringUtils.getBean("kpiFileProcess" + typeStr + "Impl");
        this.mongoTemplate = (MongoTemplate) SpringUtils.getBean("mongoTemplate");
        this.imsFiveKpi = imsFiveKpi;
        this.typeStr = typeStr;
        this.formulas = formulas;
        this.date = date;
        this.log = log;
        this.imskpiDownloadDir = imskpiDownloadDir;
    }

    @Override
    public void run() {
        List<EnipMeasInfo> curDataObj = getData(0, this.imsFiveKpi);
        countAndInsertToDB(curDataObj);
    }

    private List<EnipMeasInfo> getData(int delay, ImsFiveKpi imsFiveKpi) {
        try {
            String filePathString = this.imskpiDownloadDir + File.separator + this.typeStr + File.separator + imsFiveKpi.getId() + File.separator;
            File file = downloadFilesByFTP(imsFiveKpi, delay, filePathString);
            if (file != null) {
                return filterAndGetData(file);
            } else {
                log.info(" null");
            }
        } catch (Exception e) {
            log.error("KpiDataProcessTask getData method:" + e.getMessage() + "netType is :" + this.typeStr, e);
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 从ftp服务器下载文件
     *
     * @param imsFiveKpi
     * @param delay
     * @return
     * @throws Exception
     */
    private File downloadFilesByFTP(ImsFiveKpi imsFiveKpi, int delay, String filePathString) throws Exception {
        File filePath = new File(filePathString);
        FileUtil.newDirs(filePath);
        // 删除上一批次下载的文件
        if (filePath.exists()) {
            File[] oldxmlTargetFiles = filePath.listFiles();
            for (File file : oldxmlTargetFiles) {
                if (file.exists() && file.isFile()) {
                    log.info("|||||||||||||||||||||||||||||||||||||||||||||||" + file.getName());
                    file.delete();
                }
            }
        }
        return kpiFileProcess.download(imsFiveKpi, filePathString,log);
    }

    /**
     * 通过各个kpiFileProcess的实现类的analysis方法解析文件
     *
     * @param file
     * @return
     */
    private List<EnipMeasInfo> filterAndGetData(File file) {
        //为CSCF类型网元解析多维分析的数据
        if(this.typeStr.equals("CSCFFIVE")){
            try {
                cscfMultiAnalysis(file);
            }catch(Exception ex){
                log.error("cscfMultiAnalysis:"+ex.getMessage());
            }
            try {
                cscfMultiAnalysis_session(file);
            }catch(Exception ex){
                log.error("cscfMultiAnalysis:"+ex.getMessage());
            }
        }
        return kpiFileProcess.analysis(file, log);
    }

    private void countAndInsertToDB(Object curDataObj) {
        if (curDataObj == null) {
            log.error(typeStr+":"+imsFiveKpi.getName()+"curDataObj null");
            return;
        }
        List<EnipMeasInfo> measInfos = (List<EnipMeasInfo>) curDataObj;
        String beginTime = measInfos.get(0).getBeginTime();
        //循环每一个公式
        Map<String, String> resultMap = new HashMap<>();
        EnipKpiDataResult kpiDataResult = new EnipKpiDataResult();
        try {
            for (EnipFormula formula : formulas) {
                try {
                    //1 获取编译后的表达式对象
                    Expression compiledExp = formula.getExpression();
                    //2 取出计算因子
                    List<String> vars = compiledExp.getVariableFullNames();
                    //计算公式用的map,key 为计算因子，value 为 对应的值
                    Map<String, Object> dataMap = new HashMap<>();
                    for (String var : vars) {
                        String perVar = FormulaConvertImsFive.restoreFormula(var);
                        for (EnipMeasInfo m : measInfos) {
                            String result = parseData(m, perVar);
                            if (null != result && !result.equals("0.0")) {
                                dataMap.put(var, Double.valueOf(result));
                            }
                        }
                        if (!dataMap.containsKey(var)) {
                            dataMap.put(var, Double.valueOf("0.0"));
                        }
                    }
                    if (dataMap.size() > 0) {
                        Object result = AviatorUtil.execute(compiledExp, dataMap, formula);
                        resultMap.put(String.valueOf(formula.getId()), String.valueOf(result));
                    }
                } catch (Exception ex) {
                    log.error(typeStr+":"+imsFiveKpi.getName()+ex.getCause());
                }
            }
        } catch (Exception e) {
            log.error(typeStr+":"+imsFiveKpi.getName()+e.getCause());
        }
        if(resultMap.size()>0) {
            kpiDataResult.setDateTime(beginTime);
            kpiDataResult.setNetid(String.valueOf(imsFiveKpi.getId()));
            kpiDataResult.setPoolid(String.valueOf(imsFiveKpi.getPool_id()));
            kpiDataResult.setNetName(imsFiveKpi.getName());
            kpiDataResult.setKpiMap(resultMap);
            log.info("insert data to MongoDB " + typeStr+":"+imsFiveKpi.getName() + " start ...");
            mongoTemplate.insert(kpiDataResult, "imskpi_6_" + typeStr.replace("FIVE","").toLowerCase());
            log.info("insert data to MongoDB " + typeStr+":"+imsFiveKpi.getName() + " end ...");
        }else{
            log.info("nodata："+imsFiveKpi.getName());
        }
    }


    /**
     * 根据公式中的规则 解析数据
     *
     * @param m
     * @param perVar
     * @return
     */
    private String parseData(EnipMeasInfo m, String perVar) {
        Map<String, EnipMeasValue> mvMap = m.getMeasValueMap();
        String tempVar = perVar;
        int count = 0;
        double calResult = 0;
        if (perVar.contains("avg_")) {//判断公式是否需要计算平均值
            perVar = perVar.replace("avg_", "");
        }
        if (perVar.contains(".") && m.getMeasTypeMap().containsKey(perVar.split("\\.")[0])) {//包含条件
            String[] temp = perVar.split("\\.");
            for (String key : mvMap.keySet()) {
                boolean remark = true;
                for (int n = 1; n < temp.length; n++) {
                    if(temp[n].contains(FormulaConvertImsFive.__POINT__CHAR__)){
                        temp[n]=temp[n].replaceAll(FormulaConvertImsFive.__POINT__CHAR__,".");
                    }
                    if (!temp[n].contains("|")) {//条件中不包含|
                        if ((!temp[n].contains("not_contains")||!temp[n].contains("not_startingwith")) && !checkKey(key,temp[n].replace("contains", "").replace("startingwith",""),temp[n])) {
                            remark = false;
                            break;
                        } else if ((temp[n].contains("not_contains")||temp[n].contains("not_startingwith")) && checkKey(key,temp[n].replace("not_contains", "").replace("not_startingwith",""),temp[n])) {
                            remark = false;
                            break;
                        }
                    } else {//条件中包含|
                        String[] tempOr = temp[n].split("|");
                        for (int o = 0; o < tempOr.length; o++) {
                            if ((!tempOr[o].contains("not_contains")||!temp[n].contains("not_startingwith")) && !checkKey(key,temp[n].replace("contains", "").replace("startingwith",""),temp[n])) {
                                remark = false;
                                break;
                            } else if ((temp[n].contains("not_contains")||temp[n].contains("not_startingwith")) && checkKey(key,temp[n].replace("not_contains", "").replace("not_startingwith",""),temp[n])) {
                                remark = false;
                                break;
                            }
                        }
                    }
                }
                if (remark == true) {
                    EnipMeasValue enipMeasValue = mvMap.get(key);
                    if (enipMeasValue.getRMap().containsKey(m.getMeasTypeMap().get(temp[0]))) {
                        calResult = calResult + Double.parseDouble(enipMeasValue.getRMap().get(m.getMeasTypeMap().get(temp[0])));
                        if (tempVar.contains("avg_")) {//包含计算平均值的  计数器
                            count = count + 1;
                        }
                    }
                }
            }
            if (count > 0) {
                return calResult / count + "";
            } else {
                return calResult + "";
            }
        } else {//不包含条件
            if (m.getMeasTypeMap().containsKey(perVar)) {
                for (EnipMeasValue enipMeasValue : mvMap.values()) {
                    if (enipMeasValue.getRMap().containsKey(m.getMeasTypeMap().get(perVar))) {
                        calResult = calResult + Double.parseDouble(enipMeasValue.getRMap().get(m.getMeasTypeMap().get(perVar)));
                        if (tempVar.contains("avg_")) {//包含计算平均值的  计数器
                            count = count + 1;
                        }
                    }
                }
                if (count > 0) {
                    return calResult / count + "";
                } else {
                    return calResult + "";
                }
            }
        }
        return null;
    }

    private boolean checkKey(String key,String value,String valueSelf){
        if(key.contains(",")) {
            String[] keyStr = key.split(",");
            boolean result=false;
            for(int i=0;i<keyStr.length;i++){
                if(valueSelf.contains("contains")){
                    if (keyStr[i].trim().contains(value)) {
                        result = true;
                        break;
                    }
                }else if(valueSelf.contains("startingwith")){
                    if (keyStr[i].trim().startsWith(value)) {
                        result = true;
                        break;
                    }
                }else {
                    if (keyStr[i].trim().equals(value)) {
                        result = true;
                        break;
                    }
                }
            }
            return result;
        }else{
            return key.contains(value);
        }
    }

    /**
     * 注册成功率=注册成功次数/注册尝试次数
     * 注册成功次数
     * <measInfo measInfoId="CscfRegistrationStatistics">下 cscfAcceptedRegistrations
     * 注册尝试次数
     * 汇总各个接入类型的注册尝试次数-汇总各个接入类型的401次数
     * <measInfo measInfoId="CscfRegistrationStatistics">下 scscfAttemptedRegistrationPerAccess.SUM-scscfFailedRegistrationPerAccess.{401}
     *
     *
     * 重注册成功率=重注册成功次数/重注册尝试次数
     * 重注册成功次数
     * <measInfo measInfoId="CscfRegistrationStatistics">下 cscfAcceptedReregistrations
     * 重注册尝试次数
     * 重注册成功次数次数-汇总失败次数
     * <measInfo measInfoId="CscfRegistrationStatistics">下 cscfAcceptedReregistrations + cscfReregistrationsFailure.{sum}
     *
     * 去注册成功率=去注册成功次数/去注册尝试次数
     * 去注册成功次数
     * <measInfo measInfoId="CscfRegistrationStatistics">下 cscfAcceptedDeregistrations
     * 去注册尝试次数
     * 去注册成功次数次数-汇总失败次数
     * <measInfo measInfoId="CscfRegistrationStatistics">下 cscfAcceptedDeregistrations + cscfDeregistrationsFailure.{sum}
     * CSCF类型多维分析
     * @param file
     */
    public void cscfMultiAnalysis(File file) {
        //插入mongodb的對象
        ImsCscfMulti imsCscfMulti=new ImsCscfMulti();
        imsCscfMulti.setNetid(this.imsFiveKpi.getId().toString());
        List<MultiHexChildEntity> hexChildEntityList=new ArrayList<>();
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(file);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element measData = doc.getRootElement().element("measData");
        String beginTime = doc.getRootElement().element("fileHeader").element("measCollec").attributeValue("beginTime");
        beginTime = beginTime.substring(0, beginTime.indexOf("+")).replace("T", " ");
        imsCscfMulti.setDateTime(beginTime);
        List<?> measInfoList = measData.elements("measInfo");

        //定义变量 用于记录每个文件中关键字对应的P值
        String pCscfSipErrorResponse="";//ppt p=64
        String pCscfInviteCommunicationFailure="";//ppt p=28
        String pScscfSuccessfulRegistrationPerAccess="";//ppt p=4
        String pScscfFailedRegistrationPerAccess="";//ppt p=10
        String pScscfSuccessfulReregistrationPerAccess="";//ppt p=5
        String pScscfFailedReregistrationPerAccess="";//ppt p=11
        String pScscfSuccessfulDeregistrationPerAccess="";//ppt p=7
        String pScscfFailedDeregistrationPerAccess="";// ppt p=9
        String pScscfRegistrationsFromVisitingDomainsFailure="";//ppt p=23
        String pScscfRegistrationsFromVisitingDomainsSuccess="";//ppt p=24

        String  cscfAcceptedRegistrations="";//ppt  cscfAcceptedRegistrations
        String scscfAttemptedRegistrationPerAccess=""; //ppt scscfAttemptedRegistrationPerAccess
        String cscfAcceptedReregistrations="";//ppt cscfAcceptedReregistrations
        String cscfReregistrationsFailure="";//ppt cscfReregistrationsFailure
        String cscfAcceptedDeregistrations="";//ppt cscfAcceptedDeregistrations
        String cscfDeregistrationsFailure="";//ppt cscfDeregistrationsFailure

        //解析文件
        for (Iterator<?> it = measInfoList.iterator(); it.hasNext();) {
            Element measInfo = (Element) it.next();
            int remark=0;

            //解析CscfRegistrationStatistics节点
            if(measInfo.attributeValue("measInfoId").equals("CscfRegistrationStatistics")){
                MultiEntity registerMultiEntity=new MultiEntity();
                List<MultiAccessChildEntity> successMultiAccessChildEntityList=new ArrayList<>();
                List<MultiAccessChildEntity> faildMultiAccessChildEntityList=new ArrayList<>();
                List<MultiDomainChildEntity> domainSuccessMultiChildEntityList=new ArrayList<>();
                List<MultiDomainChildEntity> domainFaildMultiChildEntityList=new ArrayList<>();
                MultiEntity registerReMultiEntity=new MultiEntity();
                List<MultiAccessChildEntity> successMultiAccessChildEntityListRE=new ArrayList<>();
                List<MultiAccessChildEntity> faildMultiAccessChildEntityListRE=new ArrayList<>();
                MultiEntity registerDeMultiEntity=new MultiEntity();
                List<MultiAccessChildEntity> successMultiAccessChildEntityListDE=new ArrayList<>();
                List<MultiAccessChildEntity> faildMultiAccessChildEntityListDE=new ArrayList<>();
                remark=remark+1;//用于减少循环
                //解析关键字对应的P值
                List<?> measTypeList=measInfo.elements("measType");
                for (Iterator<?> measTypeIt = measTypeList.iterator(); measTypeIt.hasNext();) {
                    Element measType = (Element) measTypeIt.next();
                    if(measType.getTextTrim().equals("cscfInviteCommunicationFailure")){
                        pCscfInviteCommunicationFailure=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfSuccessfulRegistrationPerAccess")){
                        pScscfSuccessfulRegistrationPerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfFailedRegistrationPerAccess")){
                        pScscfFailedRegistrationPerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfSuccessfulReregistrationPerAccess")){
                        pScscfSuccessfulReregistrationPerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfFailedReregistrationPerAccess")){
                        pScscfFailedReregistrationPerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfSuccessfulDeregistrationPerAccess")){
                        pScscfSuccessfulDeregistrationPerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfFailedDeregistrationPerAccess")){
                        pScscfFailedDeregistrationPerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfRegistrationsFromVisitingDomainsFailure")){
                        pScscfRegistrationsFromVisitingDomainsFailure=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfRegistrationsFromVisitingDomainsSuccess")){
                        pScscfRegistrationsFromVisitingDomainsSuccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("cscfAcceptedRegistrations")){
                        cscfAcceptedRegistrations=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfAttemptedRegistrationPerAccess")){
                        scscfAttemptedRegistrationPerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("cscfAcceptedReregistrations")){
                        cscfAcceptedReregistrations=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("cscfReregistrationsFailure")){
                        cscfReregistrationsFailure=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("cscfAcceptedDeregistrations")){
                        cscfAcceptedDeregistrations=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("cscfDeregistrationsFailure")){
                        cscfDeregistrationsFailure=measType.attributeValue("p");
                    }
                }
                //解析P值对应的value
                List<?> measValueList=measInfo.elements("measValue");
                for (Iterator<?> measValueIt = measValueList.iterator(); measValueIt.hasNext();) {
                    Element measValue = (Element) measValueIt.next();
                    String measObjLdn=measValue.attributeValue("measObjLdn");
                    boolean sum=measObjLdn.equals("SUM");
                    List<?> rList = measValue.elements("r");
                    for (Iterator<?> r = rList.iterator(); r.hasNext(); ) {
                        Element rNode = (Element) r.next();
                        String p=rNode.attributeValue("p");
                        MultiAccessChildEntity multiAccessChildEntity=new MultiAccessChildEntity();
                        MultiDomainChildEntity multiDomainChildEntity=new MultiDomainChildEntity();
                                if(p.equals(pScscfSuccessfulRegistrationPerAccess)) {
                                    if (!sum) {
                                        multiAccessChildEntity.setAccessType(measObjLdn);
                                        multiAccessChildEntity.setValue(rNode.getTextTrim());
                                        successMultiAccessChildEntityList.add(multiAccessChildEntity);
                                    }
                                }else if(p.equals(pScscfFailedRegistrationPerAccess)) {
                                    if (measObjLdn.contains("AccessType") && measObjLdn.contains("SIPResponseCode")) {
                                        String[] meas = measObjLdn.split(",");
                                        multiAccessChildEntity.setAccessType(meas[0].split("=")[1]);
                                        multiAccessChildEntity.setSipCode(meas[1].split("=")[1]);
                                        multiAccessChildEntity.setValue(rNode.getTextTrim());
                                        faildMultiAccessChildEntityList.add(multiAccessChildEntity);
                                        if(multiAccessChildEntity.getSipCode().equals("401")){
                                            if(null==registerMultiEntity.getTotalSip401()||"".equals(registerMultiEntity.getTotalSip401())){
                                                registerMultiEntity.setTotalSip401(rNode.getTextTrim());
                                            }else{
                                                registerMultiEntity.setTotalSip401(String.valueOf(Long.parseLong(registerMultiEntity.getTotalSip401())+Long.parseLong(rNode.getTextTrim())));
                                            }
                                        }
                                    }
                                }else if(p.equals(pScscfSuccessfulReregistrationPerAccess)) {
                                    if (!sum) {
                                        multiAccessChildEntity.setAccessType(measObjLdn);
                                        multiAccessChildEntity.setValue(rNode.getTextTrim());
                                        successMultiAccessChildEntityListRE.add(multiAccessChildEntity);
                                    }
                                }else if(p.equals(pScscfFailedReregistrationPerAccess)) {
                                    if (measObjLdn.contains("AccessType") && measObjLdn.contains("SIPResponseCode")) {
                                        String[] meas = measObjLdn.split(",");
                                        multiAccessChildEntity.setAccessType(meas[0].split("=")[1]);
                                        multiAccessChildEntity.setSipCode(meas[1].split("=")[1]);
                                        multiAccessChildEntity.setValue(rNode.getTextTrim());
                                        faildMultiAccessChildEntityListRE.add(multiAccessChildEntity);
                                    }
                                }else if(p.equals(pScscfSuccessfulDeregistrationPerAccess)) {
                                    if (!sum) {
                                        multiAccessChildEntity.setAccessType(measObjLdn);
                                        multiAccessChildEntity.setValue(rNode.getTextTrim());
                                        successMultiAccessChildEntityListDE.add(multiAccessChildEntity);
                                    }
                                }else if(p.equals(pScscfFailedDeregistrationPerAccess)) {
                                    if (measObjLdn.contains("AccessType") && measObjLdn.contains("SIPResponseCode")) {
                                        String[] meas = measObjLdn.split(",");
                                        multiAccessChildEntity.setAccessType(meas[0].split("=")[1]);
                                        multiAccessChildEntity.setSipCode(meas[1].split("=")[1]);
                                        multiAccessChildEntity.setValue(rNode.getTextTrim());
                                        faildMultiAccessChildEntityListDE.add(multiAccessChildEntity);
                                    }
                                }else if(p.equals(pCscfInviteCommunicationFailure)) {
                                    if (measObjLdn.contains("SIPResponseCodePhraseAndHexErrorCode")) {
                                        String[] meas = measObjLdn.split(" ");
                                        if (!"".equals(meas[meas.length - 1]) && (meas[meas.length - 1].startsWith("01") || meas[meas.length - 1].startsWith("11")) && meas[0].contains("=")) {
                                            MultiHexChildEntity multiHexChildEntity = new MultiHexChildEntity();
                                            multiHexChildEntity.setHexCode(meas[meas.length - 1]);
                                            multiHexChildEntity.setSipCode(meas[0].split("=")[1]);
                                            multiHexChildEntity.setValue(rNode.getTextTrim());
                                            hexChildEntityList.add(multiHexChildEntity);
                                        }
                                    }
                                }else if(p.equals(pScscfRegistrationsFromVisitingDomainsFailure)) {
                                    if (measObjLdn.contains("DomainName") && measObjLdn.contains("SIPResponseCode")) {
                                        String[] meas = measObjLdn.split(",");
                                        multiDomainChildEntity.setDomainName(meas[0].split("=")[1]);
                                        multiDomainChildEntity.setSipCode(meas[1].split("=")[1]);
                                        multiDomainChildEntity.setValue(rNode.getTextTrim());
                                        domainFaildMultiChildEntityList.add(multiDomainChildEntity);
                                    }
                                }else if(p.equals(pScscfRegistrationsFromVisitingDomainsSuccess)) {
                                    if (measObjLdn.contains("DomainName")) {
                                        multiDomainChildEntity.setDomainName(measObjLdn.split("=")[1]);
                                        multiDomainChildEntity.setValue(rNode.getTextTrim());
                                        domainSuccessMultiChildEntityList.add(multiDomainChildEntity);
                                    }
                                }else if(p.equals(cscfAcceptedRegistrations)) {
                                    if (!sum) {
                                        if(null==registerMultiEntity.getTotalSucc()||"".equals(registerMultiEntity.getTotalSucc())){
                                            registerMultiEntity.setTotalSucc(rNode.getTextTrim());
                                        }else{
                                            registerMultiEntity.setTotalSucc(String.valueOf(Long.parseLong(registerMultiEntity.getTotalSucc())+Long.parseLong(rNode.getTextTrim())));
                                        }
                                    }
                                }else if(p.equals(scscfAttemptedRegistrationPerAccess)) {
                                    if (!sum) {
                                        if(null==registerMultiEntity.getTotalAttem()||"".equals(registerMultiEntity.getTotalAttem())){
                                            registerMultiEntity.setTotalAttem(rNode.getTextTrim());
                                        }else{
                                            registerMultiEntity.setTotalAttem(String.valueOf(Long.parseLong(registerMultiEntity.getTotalAttem())+Long.parseLong(rNode.getTextTrim())));
                                        }
                                    }
                                }else if(p.equals(cscfAcceptedReregistrations)) {
                                    if (!sum) {
                                        if(null==registerReMultiEntity.getTotalSucc()||"".equals(registerReMultiEntity.getTotalSucc())){
                                            registerReMultiEntity.setTotalSucc(rNode.getTextTrim());
                                        }else{
                                            registerReMultiEntity.setTotalSucc(String.valueOf(Long.parseLong(registerReMultiEntity.getTotalSucc())+Long.parseLong(rNode.getTextTrim())));
                                        }
                                    }
                                }else if(p.equals(cscfReregistrationsFailure)) {
                                    if (!sum) {
                                        if(null==registerReMultiEntity.getTotalFaild()||"".equals(registerReMultiEntity.getTotalFaild())){
                                            registerReMultiEntity.setTotalFaild(rNode.getTextTrim());
                                        }else{
                                            registerReMultiEntity.setTotalFaild(String.valueOf(Long.parseLong(registerReMultiEntity.getTotalFaild())+Long.parseLong(rNode.getTextTrim())));
                                        }
                                    }
                                }else if(p.equals(cscfAcceptedDeregistrations)) {
                                    if (!sum) {
                                        if(null==registerDeMultiEntity.getTotalSucc()||"".equals(registerDeMultiEntity.getTotalSucc())){
                                            registerDeMultiEntity.setTotalSucc(rNode.getTextTrim());
                                        }else{
                                            registerDeMultiEntity.setTotalSucc(String.valueOf(Long.parseLong(registerDeMultiEntity.getTotalSucc())+Long.parseLong(rNode.getTextTrim())));
                                        }
                                    }
                                }else if(p.equals(cscfDeregistrationsFailure)) {
                                    if (!sum) {
                                        if(null==registerDeMultiEntity.getTotalFaild()||"".equals(registerDeMultiEntity.getTotalFaild())){
                                            registerDeMultiEntity.setTotalFaild(rNode.getTextTrim());
                                        }else{
                                            registerDeMultiEntity.setTotalFaild(String.valueOf(Long.parseLong(registerDeMultiEntity.getTotalFaild())+Long.parseLong(rNode.getTextTrim())));
                                        }
                                    }
                                }
                        }
                }
                registerMultiEntity.setSuccessMultiAccessChildEntityList(successMultiAccessChildEntityList);
                registerMultiEntity.setFaildMultiAccessChildEntityList(faildMultiAccessChildEntityList);
                registerMultiEntity.setDomainSuccessMultiChildEntityList(domainSuccessMultiChildEntityList);
                registerMultiEntity.setDomainFaildultiChildEntityList(domainFaildMultiChildEntityList);
                registerReMultiEntity.setSuccessMultiAccessChildEntityList(successMultiAccessChildEntityListRE);
                registerReMultiEntity.setFaildMultiAccessChildEntityList(faildMultiAccessChildEntityListRE);
                registerDeMultiEntity.setFaildMultiAccessChildEntityList(faildMultiAccessChildEntityListDE);
                registerDeMultiEntity.setSuccessMultiAccessChildEntityList(successMultiAccessChildEntityListDE);
                imsCscfMulti.setRegisterMultiEntity(registerMultiEntity);
                imsCscfMulti.setRegisterReMultiEntity(registerReMultiEntity);
                imsCscfMulti.setRegisterDeMultiEntity(registerDeMultiEntity);
            }else if(measInfo.attributeValue("measInfoId").equals("CscfSession")){
                remark=remark+1;//用于减少循环
                //解析CscfSession节点
                //解析关键字对应的P值
                List<?> measTypeList=measInfo.elements("measType");
                for (Iterator<?> measTypeIt = measTypeList.iterator(); measTypeIt.hasNext();) {
                    Element measType = (Element) measTypeIt.next();
                    if(measType.getTextTrim().equals("cscfSipErrorResponse")){
                        pCscfSipErrorResponse=measType.attributeValue("p");
                    }
                }

                List<?> measValueList=measInfo.elements("measValue");
                for (Iterator<?> measValueIt = measValueList.iterator(); measValueIt.hasNext();) {
                    Element measValue = (Element) measValueIt.next();
                    String measObjLdn=measValue.attributeValue("measObjLdn");
                    boolean SUM=measObjLdn.equals("SUM");
                    boolean sum=measObjLdn.equals("sum");
                    List<?> rList = measValue.elements("r");
                    for (Iterator<?> r = rList.iterator(); r.hasNext(); ) {
                        Element rNode = (Element) r.next();
                        String p=rNode.attributeValue("p");
                        if(p.equals(pCscfSipErrorResponse)&&!sum&&!SUM){
                            if(measObjLdn.contains("SIPMethod")&&measObjLdn.contains("SIPResponseCode")&&measObjLdn.contains("HexErrorCode")) {
                                String[] meas = measObjLdn.split(",");
                                String []hexCode=meas[2].split("=");
                                if(meas.length==3&&hexCode.length==2&&(hexCode[1].startsWith("01")||hexCode[1].startsWith("11"))){
                                    MultiHexChildEntity multiHexChildEntity=new MultiHexChildEntity();
                                    multiHexChildEntity.setHexCode(hexCode[1]);
                                    multiHexChildEntity.setSipCode(meas[1].split("=")[1]);
                                    multiHexChildEntity.setValue(rNode.getTextTrim());
                                    multiHexChildEntity.setSipMethod(meas[0].split("=")[1]);
                                    hexChildEntityList.add(multiHexChildEntity);
                                }
                            }
                        }
                    }
                }
            }
            //measInfo处理完需要的节点后跳出
            if(remark==2){
                break;
            }
        }
        imsCscfMulti.setHexChildEntityList(hexChildEntityList);
        this.log.info("insert multi:"+this.imsFiveKpi.getId()+"start");
        mongoTemplate.insert(imsCscfMulti,"ims_cscf_multi_register");
        this.log.info("insert multi:"+this.imsFiveKpi.getId()+"end");
    }

    /**
     * CSCF 会话多维分析
     * @param file
     */
    public void cscfMultiAnalysis_session(File file) {
        ImsCscfMultiSession imsCscfMultiSession=new ImsCscfMultiSession();
        imsCscfMultiSession.setNetid(this.imsFiveKpi.getId().toString());
        SAXReader reader = new SAXReader();
        Document doc = null;
        try {
            doc = reader.read(file);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element measData = doc.getRootElement().element("measData");
        String beginTime = doc.getRootElement().element("fileHeader").element("measCollec").attributeValue("beginTime");
        beginTime = beginTime.substring(0, beginTime.indexOf("+")).replace("T", " ");
        imsCscfMultiSession.setDateTime(beginTime);
        List<?> measInfoList = measData.elements("measInfo");

        //定义变量用于记录关键字对应的P值
        //S-CSCF主叫接通率
        String pScscfOriginatingInviteSuccessfulEstablishedToAs="";
        String pScscfOriginatingInviteSuccessfulEstablishedNoAs="";
        String pCscfOriginatingInviteToAsAttempts="";
        String pScscfOriginatingInviteNoAsAttempts="";
        //S-CSCF主叫会话183原因值分析
        String pScscfOrig183FailedSessionInEarlyDialogPerAccess="";
        //S-CSCF主叫会话失败原因分析
        String pScscfOrigFailedInvitePerAccess="";
        //S-CSCF主叫会话平均建立时长
        String pScscfAccumulatedOriginatingSessionEstablishmentTime="";
        //主叫会话统计多维分析--接入类型维度分析
        String pScscfOrigSuccessfulEstablishedInvitePerAccess="";
        String pScscfOrigAttemptedInvitePerAccess="";
        //主叫会话统计多维分析--接入类型+媒体类型维度分析
        String pScscfOrigSuccessfulEstablishedInvitePerMediaAndAccess="";
        String pScscfOrigAttemptedInvitePerMediaAndAccess="";
        //主叫会话Q.850原因值--接入类型+媒体类型维度分析
        String pScscfOrig183FailedSessionInEarlyDialogPerMediaAndAccess="";
        //主叫会话SIP返回码--媒体类型维度分析
        String pScscfOrigFailedSessionPerMedia="";

        //S-CSCF被叫接通率
        String pScscfTerminatingInviteSuccessfulEstablishedToAs="";
        String pScscfTerminatingInviteSuccessfulEstablishedNoAs="";
        String pCscfTerminatingInviteToAsAttempts ="";
        String pScscfTerminatingInviteNoAsAttempts="";
        //S-CSCF被叫会话183原因值分析
        String pScscfTerm183FailedSessionInEarlyDialogPerAccess="";
        //S-CSCF被叫会话失败原因分析
        String pScscfTermFailedInvitePerAccess="";
        //S-CSCF被叫会话平均建立时长
        String pScscfAccumulatedTerminatingSessionEstablishmentTime="";
        //被叫会话统计多维分析--接入类型维度分析
        String pScscfTermSuccessfulEstablishedInvitePerAccess="";
        String pScscfTermAttemptedInvitePerAccess="";
        //主叫会话统计多维分析--接入类型+媒体类型维度分析
        String pScscfTermSuccessfulEstablishedInvitePerMediaAndAccess="";
        String pScscfTermAttemptedInvitePerMediaAndAccess="";
        //主叫会话Q.850原因值--接入类型+媒体类型维度分析
        String pScscfTerm183FailedSessionInEarlyDialogPerMediaAndAccess="";
        //主叫会话SIP返回码--媒体类型维度分析
        String pScscfTermFailedSessionPerMedia="";

        //SIP返回码 408错误原因分布
        String pCscfSipErrorResponse="";
        String pCscfInviteCommunicationFailure="";

        //主叫会话数据
        MultiSessionEntity origiMultiSessionEntity=new MultiSessionEntity();
        List<MultiSessionChildEntity> origiSession183FailList=new ArrayList<>();
        List<MultiSessionChildEntity> origiSessionFailList=new ArrayList<>();
        List<MultiSessionChildEntity> origiSessionSuccList=new ArrayList<>();
        List<MultiSessionChildEntity> origiSessionSuccAttemList=new ArrayList<>();
        List<MultiSessionChildEntity> origiSessionSuccMediaAndAccessList=new ArrayList<>();
        List<MultiSessionChildEntity> origiSessionSuccAttemMediaAndAccessList=new ArrayList<>();
        List<MultiSessionChildEntity> origiSession183FailMediaAndAccessList=new ArrayList<>();
        List<MultiSessionChildEntity> origiSessionFailMediaList=new ArrayList<>();
        List<MultiSessionChildEntity> origiSessionHexList=new ArrayList<>();
        //被叫会话数据
        MultiSessionEntity termiMultiSessionEntity=new MultiSessionEntity();
        List<MultiSessionChildEntity> termiSession183FailList=new ArrayList<>();
        List<MultiSessionChildEntity> termiSessionFailList=new ArrayList<>();
        List<MultiSessionChildEntity> termiSessionSuccList=new ArrayList<>();
        List<MultiSessionChildEntity> termiSessionSuccAttemList=new ArrayList<>();
        List<MultiSessionChildEntity> termiSessionSuccMediaAndAccessList=new ArrayList<>();
        List<MultiSessionChildEntity> termiSessionSuccAttemMediaAndAccessList=new ArrayList<>();
        List<MultiSessionChildEntity> termiSession183FailMediaAndAccessList=new ArrayList<>();
        List<MultiSessionChildEntity> termiSessionFailMediaList=new ArrayList<>();
        List<MultiSessionChildEntity> termiSessionHexList=new ArrayList<>();

        //解析文件
        for (Iterator<?> it = measInfoList.iterator(); it.hasNext();) {
            Element measInfo = (Element) it.next();
            int remark = 0;

            if(measInfo.attributeValue("measInfoId").equals("CscfSession")){
                remark=remark+1;//用于减少循环
                //解析CscfSession节点
                //解析关键字对应的P值
                List<?> measTypeList=measInfo.elements("measType");
                for (Iterator<?> measTypeIt = measTypeList.iterator(); measTypeIt.hasNext();) {
                    Element measType = (Element) measTypeIt.next();
                    if(measType.getTextTrim().equals("scscfOriginatingInviteSuccessfulEstablishedToAs")){
                        pScscfOriginatingInviteSuccessfulEstablishedToAs=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfOriginatingInviteSuccessfulEstablishedNoAs")){
                        pScscfOriginatingInviteSuccessfulEstablishedNoAs=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("cscfOriginatingInviteToAsAttempts")){
                        pCscfOriginatingInviteToAsAttempts=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfOriginatingInviteNoAsAttempts")){
                        pScscfOriginatingInviteNoAsAttempts=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfOrig183FailedSessionInEarlyDialogPerAccess")){
                        pScscfOrig183FailedSessionInEarlyDialogPerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfOrigFailedInvitePerAccess")){
                        pScscfOrigFailedInvitePerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfAccumulatedOriginatingSessionEstablishmentTime")){
                        pScscfAccumulatedOriginatingSessionEstablishmentTime=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfOrigSuccessfulEstablishedInvitePerAccess")){
                        pScscfOrigSuccessfulEstablishedInvitePerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfOrigAttemptedInvitePerAccess")){
                        pScscfOrigAttemptedInvitePerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfOrigSuccessfulEstablishedInvitePerMediaAndAccess")){
                        pScscfOrigSuccessfulEstablishedInvitePerMediaAndAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfOrigAttemptedInvitePerMediaAndAccess")){
                        pScscfOrigAttemptedInvitePerMediaAndAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfOrig183FailedSessionInEarlyDialogPerMediaAndAccess")){
                        pScscfOrig183FailedSessionInEarlyDialogPerMediaAndAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfOrigFailedSessionPerMedia")){
                        pScscfOrigFailedSessionPerMedia=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfTerminatingInviteSuccessfulEstablishedToAs")){
                        pScscfTerminatingInviteSuccessfulEstablishedToAs=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfTerminatingInviteSuccessfulEstablishedNoAs")){
                        pScscfTerminatingInviteSuccessfulEstablishedNoAs=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("cscfTerminatingInviteToAsAttempts")){
                        pCscfTerminatingInviteToAsAttempts=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfTerminatingInviteNoAsAttempts")){
                        pScscfTerminatingInviteNoAsAttempts=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfTerm183FailedSessionInEarlyDialogPerAccess")){
                        pScscfTerm183FailedSessionInEarlyDialogPerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfTermFailedInvitePerAccess")){
                        pScscfTermFailedInvitePerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfAccumulatedTerminatingSessionEstablishmentTime")){
                        pScscfAccumulatedTerminatingSessionEstablishmentTime=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfTermSuccessfulEstablishedInvitePerAccess")){
                        pScscfTermSuccessfulEstablishedInvitePerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfTermAttemptedInvitePerAccess")){
                        pScscfTermAttemptedInvitePerAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfTermSuccessfulEstablishedInvitePerMediaAndAccess")){
                        pScscfTermSuccessfulEstablishedInvitePerMediaAndAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfTermAttemptedInvitePerMediaAndAccess")){
                        pScscfTermAttemptedInvitePerMediaAndAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfTerm183FailedSessionInEarlyDialogPerMediaAndAccess")){
                        pScscfTerm183FailedSessionInEarlyDialogPerMediaAndAccess=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("scscfTermFailedSessionPerMedia")){
                        pScscfTermFailedSessionPerMedia=measType.attributeValue("p");
                    }else if(measType.getTextTrim().equals("cscfSipErrorResponse")){
                        pCscfSipErrorResponse=measType.attributeValue("p");
                    }
                }
                //解析数值
                List<?> measValueList=measInfo.elements("measValue");
                for (Iterator<?> measValueIt = measValueList.iterator(); measValueIt.hasNext();) {
                    Element measValue = (Element) measValueIt.next();
                    String measObjLdn = measValue.attributeValue("measObjLdn");
                    boolean sum = measObjLdn.equals("sum");
                    boolean SUM = measObjLdn.equals("SUM");
                    List<?> rList = measValue.elements("r");
                    for (Iterator<?> r = rList.iterator(); r.hasNext(); ) {
                        MultiSessionChildEntity multiSessionChildEntity=new MultiSessionChildEntity();
                        Element rNode = (Element) r.next();
                        String p = rNode.attributeValue("p");
                        if((p.equals(pScscfOriginatingInviteSuccessfulEstablishedToAs)||p.equals(pScscfOriginatingInviteSuccessfulEstablishedNoAs))&&sum){
                            if(null!=origiMultiSessionEntity.getTotalSucc()&&!"".equals(origiMultiSessionEntity.getTotalSucc())){
                                origiMultiSessionEntity.setTotalSucc(String.valueOf(Long.parseLong(origiMultiSessionEntity.getTotalSucc())+Long.parseLong(rNode.getTextTrim())));
                            }else{
                                origiMultiSessionEntity.setTotalSucc(rNode.getTextTrim());
                            }
                            origiMultiSessionEntity.setTotalSessionTime(origiMultiSessionEntity.getTotalSucc());
                        }else if((p.equals(pCscfOriginatingInviteToAsAttempts)||p.equals(pScscfOriginatingInviteNoAsAttempts))&&!sum&&!SUM){
                            if(null!=origiMultiSessionEntity.getTotalAttem()&&!"".equals(origiMultiSessionEntity.getTotalAttem())){
                                origiMultiSessionEntity.setTotalAttem(String.valueOf(Long.parseLong(origiMultiSessionEntity.getTotalAttem())+Long.parseLong(rNode.getTextTrim())));
                            }else{
                                origiMultiSessionEntity.setTotalAttem(rNode.getTextTrim());
                            }
                        }else if(p.equals(pScscfOrig183FailedSessionInEarlyDialogPerAccess)&&!sum&&!SUM){
                                String[] meas = measObjLdn.split(",");
                                multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                                multiSessionChildEntity.setReason(meas[1].split("=")[1]);
                                multiSessionChildEntity.setValue(rNode.getTextTrim());
                            if(!multiSessionChildEntity.getReason().contains("DEFAULT")) {
                                origiSession183FailList.add(multiSessionChildEntity);
                            }
                        }else if(p.equals(pScscfOrigFailedInvitePerAccess)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setSipCode(meas[1].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            origiSessionFailList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfAccumulatedOriginatingSessionEstablishmentTime)&&!sum&&!SUM){
                            if(null!=origiMultiSessionEntity.getTotalAccumuTime()&&!"".equals(origiMultiSessionEntity.getTotalAccumuTime())){
                                origiMultiSessionEntity.setTotalAccumuTime(String.valueOf(Long.parseLong(origiMultiSessionEntity.getTotalAccumuTime())+Long.parseLong(rNode.getTextTrim())));
                            }else{
                                origiMultiSessionEntity.setTotalAccumuTime(rNode.getTextTrim());
                            }
                        }else if(p.equals(pScscfOrigSuccessfulEstablishedInvitePerAccess)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setSipCode(meas[1].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            origiSessionSuccList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfOrigAttemptedInvitePerAccess)&&!sum&&!SUM){
                            multiSessionChildEntity.setAccessType(measObjLdn);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            origiSessionSuccAttemList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfOrigSuccessfulEstablishedInvitePerMediaAndAccess)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setMediaType(meas[1].split("=")[1]);
                            multiSessionChildEntity.setSipCode(meas[2].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            origiSessionSuccMediaAndAccessList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfOrigAttemptedInvitePerMediaAndAccess)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setMediaType(meas[1].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            origiSessionSuccAttemMediaAndAccessList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfOrig183FailedSessionInEarlyDialogPerMediaAndAccess)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setMediaType(meas[1].split("=")[1]);
                            multiSessionChildEntity.setReason(meas[2].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            origiSession183FailMediaAndAccessList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfOrigFailedSessionPerMedia)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setMediaType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setSipCode(meas[1].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            origiSessionFailMediaList.add(multiSessionChildEntity);
                        }else if((p.equals(pScscfTerminatingInviteSuccessfulEstablishedToAs)||p.equals(pScscfTerminatingInviteSuccessfulEstablishedNoAs))&&sum){
                            if(null!=termiMultiSessionEntity.getTotalSucc()&&!"".equals(termiMultiSessionEntity.getTotalSucc())){
                                termiMultiSessionEntity.setTotalSucc(String.valueOf(Long.parseLong(termiMultiSessionEntity.getTotalSucc())+Long.parseLong(rNode.getTextTrim())));
                            }else{
                                termiMultiSessionEntity.setTotalSucc(rNode.getTextTrim());
                            }
                            termiMultiSessionEntity.setTotalSessionTime(termiMultiSessionEntity.getTotalSucc());
                        }else if((p.equals(pCscfTerminatingInviteToAsAttempts)||p.equals(pScscfTerminatingInviteNoAsAttempts))&&!sum&&!SUM){
                            if(null!=termiMultiSessionEntity.getTotalAttem()&&!"".equals(termiMultiSessionEntity.getTotalAttem())){
                                termiMultiSessionEntity.setTotalAttem(String.valueOf(Long.parseLong(termiMultiSessionEntity.getTotalAttem())+Long.parseLong(rNode.getTextTrim())));
                            }else{
                                termiMultiSessionEntity.setTotalAttem(rNode.getTextTrim());
                            }
                        }else if(p.equals(pScscfTerm183FailedSessionInEarlyDialogPerAccess)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setReason(meas[1].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            if(!multiSessionChildEntity.getReason().contains("DEFAULT")) {
                                termiSession183FailList.add(multiSessionChildEntity);
                            }
                        }else if(p.equals(pScscfTermFailedInvitePerAccess)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setSipCode(meas[1].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            termiSessionFailList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfAccumulatedTerminatingSessionEstablishmentTime)&&!sum&&!SUM){
                            if(null!=termiMultiSessionEntity.getTotalAccumuTime()&&!"".equals(termiMultiSessionEntity.getTotalAccumuTime())){
                                termiMultiSessionEntity.setTotalAccumuTime(String.valueOf(Long.parseLong(termiMultiSessionEntity.getTotalAccumuTime())+Long.parseLong(rNode.getTextTrim())));
                            }else{
                                termiMultiSessionEntity.setTotalAccumuTime(rNode.getTextTrim());
                            }
                        }else if(p.equals(pScscfTermSuccessfulEstablishedInvitePerAccess)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setSipCode(meas[1].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            termiSessionSuccList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfTermAttemptedInvitePerAccess)&&!sum&&!SUM){
                            multiSessionChildEntity.setAccessType(measObjLdn);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            termiSessionSuccAttemList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfTermSuccessfulEstablishedInvitePerMediaAndAccess)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setMediaType(meas[1].split("=")[1]);
                            multiSessionChildEntity.setSipCode(meas[2].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            termiSessionSuccMediaAndAccessList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfTermAttemptedInvitePerMediaAndAccess)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setMediaType(meas[1].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            termiSessionSuccAttemMediaAndAccessList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfTerm183FailedSessionInEarlyDialogPerMediaAndAccess)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setAccessType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setMediaType(meas[1].split("=")[1]);
                            multiSessionChildEntity.setReason(meas[2].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            termiSession183FailMediaAndAccessList.add(multiSessionChildEntity);
                        }else if(p.equals(pScscfTermFailedSessionPerMedia)&&!sum&&!SUM){
                            String[] meas = measObjLdn.split(",");
                            multiSessionChildEntity.setMediaType(meas[0].split("=")[1]);
                            multiSessionChildEntity.setSipCode(meas[1].split("=")[1]);
                            multiSessionChildEntity.setValue(rNode.getTextTrim());
                            termiSessionFailMediaList.add(multiSessionChildEntity);
                        }else if(p.equals(pCscfSipErrorResponse)&&!sum&&!SUM){
                            if(measObjLdn.contains("SIPMethod")&&measObjLdn.contains("SIPResponseCode")&&measObjLdn.contains("HexErrorCode")) {
                                String[] meas = measObjLdn.split(",");
                                String []hexCode=meas[2].split("=");
                                if(meas.length==3&&hexCode.length==2&&(hexCode[1].startsWith("03")||hexCode[1].startsWith("13"))){
                                    multiSessionChildEntity.setHexCode(hexCode[1]);
                                    multiSessionChildEntity.setSipCode(meas[1].split("=")[1]);
                                    multiSessionChildEntity.setValue(rNode.getTextTrim());
                                    multiSessionChildEntity.setSipMethod(meas[0].split("=")[1]);
                                    termiSessionHexList.add(multiSessionChildEntity);
                                }else if(meas.length==3&&hexCode.length==2&&(hexCode[1].startsWith("02")||hexCode[1].startsWith("12"))){
                                    multiSessionChildEntity.setHexCode(hexCode[1]);
                                    multiSessionChildEntity.setSipCode(meas[1].split("=")[1]);
                                    multiSessionChildEntity.setValue(rNode.getTextTrim());
                                    multiSessionChildEntity.setSipMethod(meas[0].split("=")[1]);
                                    origiSessionHexList.add(multiSessionChildEntity);
                                }
                            }
                        }
                    }
                }
            }else if(measInfo.attributeValue("measInfoId").equals("CscfRegistrationStatistics")){
                //解析关键字对应的P值
                List<?> measTypeList=measInfo.elements("measType");
                for (Iterator<?> measTypeIt = measTypeList.iterator(); measTypeIt.hasNext();) {
                    Element measType = (Element) measTypeIt.next();
                    if(measType.getTextTrim().equals("cscfInviteCommunicationFailure")){
                        pCscfInviteCommunicationFailure=measType.attributeValue("p");
                    }
                }

                List<?> measValueList=measInfo.elements("measValue");
                for (Iterator<?> measValueIt = measValueList.iterator(); measValueIt.hasNext();) {
                    Element measValue = (Element) measValueIt.next();
                    String measObjLdn=measValue.attributeValue("measObjLdn");
                    boolean SUM=measObjLdn.equals("SUM");
                    boolean sum=measObjLdn.equals("sum");
                    List<?> rList = measValue.elements("r");
                    for (Iterator<?> r = rList.iterator(); r.hasNext(); ) {
                        MultiSessionChildEntity multiSessionChildEntity=new MultiSessionChildEntity();
                        Element rNode = (Element) r.next();
                        String p=rNode.attributeValue("p");
                        if(p.equals(pCscfInviteCommunicationFailure)&&!sum&&!SUM) {
                            if (measObjLdn.contains("SIPResponseCodePhraseAndHexErrorCode")) {
                                String[] meas = measObjLdn.split(" ");
                                if (!"".equals(meas[meas.length - 1]) && (meas[meas.length - 1].startsWith("02") || meas[meas.length - 1].startsWith("12")) && meas[0].contains("=")) {
                                    multiSessionChildEntity.setHexCode(meas[meas.length - 1]);
                                    multiSessionChildEntity.setSipCode(meas[0].split("=")[1]);
                                    multiSessionChildEntity.setValue(rNode.getTextTrim());
                                    origiSessionHexList.add(multiSessionChildEntity);
                                }else if(!"".equals(meas[meas.length - 1]) && (meas[meas.length - 1].startsWith("03") || meas[meas.length - 1].startsWith("13")) && meas[0].contains("=")){
                                    multiSessionChildEntity.setHexCode(meas[meas.length - 1]);
                                    multiSessionChildEntity.setSipCode(meas[0].split("=")[1]);
                                    multiSessionChildEntity.setValue(rNode.getTextTrim());
                                    termiSessionHexList.add(multiSessionChildEntity);
                                }
                            }
                        }
                    }
                }
            }
            //measInfo处理完需要的节点后跳出
            if(remark==2){
                break;
            }
        }
        setListValue(origiMultiSessionEntity, origiSession183FailList, origiSessionFailList, origiSessionSuccList, origiSessionSuccAttemList, origiSessionSuccMediaAndAccessList, origiSessionSuccAttemMediaAndAccessList, origiSession183FailMediaAndAccessList, origiSessionFailMediaList, origiSessionHexList);

        setListValue(termiMultiSessionEntity, termiSession183FailList, termiSessionFailList, termiSessionSuccList, termiSessionSuccAttemList, termiSessionSuccMediaAndAccessList, termiSessionSuccAttemMediaAndAccessList, termiSession183FailMediaAndAccessList, termiSessionFailMediaList, termiSessionHexList);

        imsCscfMultiSession.setOrigMultiEntity(origiMultiSessionEntity);
        imsCscfMultiSession.setTermMultiEntity(termiMultiSessionEntity);

        this.log.info("insert multi session:"+this.imsFiveKpi.getId()+"start");
        mongoTemplate.insert(imsCscfMultiSession,"ims_cscf_multi_session");
        this.log.info("insert multi session:"+this.imsFiveKpi.getId()+"end");
    }

    private void setListValue(MultiSessionEntity multiSessionEntity, List<MultiSessionChildEntity> session183FailList, List<MultiSessionChildEntity> sessionFailList, List<MultiSessionChildEntity> sessionSuccList, List<MultiSessionChildEntity> sessionSuccAttemList, List<MultiSessionChildEntity> sessionSuccMediaAndAccessList, List<MultiSessionChildEntity> sessionSuccAttemMediaAndAccessList, List<MultiSessionChildEntity> session183FailMediaAndAccessList, List<MultiSessionChildEntity> sessionFailMediaList, List<MultiSessionChildEntity> sessionHexList) {
        multiSessionEntity.setSession183FailList(session183FailList);
        multiSessionEntity.setSessionFailList(sessionFailList);
        multiSessionEntity.setSessionSuccList(sessionSuccList);
        multiSessionEntity.setSessionSuccAttemList(sessionSuccAttemList);
        multiSessionEntity.setSessionSuccMediaAndAccessList(sessionSuccMediaAndAccessList);
        multiSessionEntity.setSessionSuccAttemMediaAndAccessList(sessionSuccAttemMediaAndAccessList);
        multiSessionEntity.setSession183FailMediaAndAccessList(session183FailMediaAndAccessList);
        multiSessionEntity.setSessionFailMediaList(sessionFailMediaList);
        multiSessionEntity.setSessionHexList(sessionHexList);
    }
}
